using System;
using System.ComponentModel;
using System.Xml.Serialization;

namespace Kotonoha.Objects.SuperMemo {
    public class ItemLearningData : IEquatable<ItemLearningData>, INotifyPropertyChanged {

        public ItemLearningData() {
            id = 0;
            difficulty = 2.5;
        }

        private long id;
        [XmlIgnore]
        public virtual long Id {
            get { return id; }
            set { id = value; }
        }

        private DateTime intervalStart;

        [XmlAttribute]
        public virtual DateTime IntervalStart {
            get { return intervalStart; }
            set {
                intervalStart = value;
                FirePropertyChanged("IntervalStart");
            }
        }

        private DateTime intervalEnd;

        [XmlAttribute]
        public virtual DateTime IntervalEnd {
            get { return intervalEnd; }
            set {
                intervalEnd = value;
                FirePropertyChanged("IntervalEnd");
            }
        }

        private double intervalLength;

        [XmlAttribute]
        public virtual double IntervalLength {
            get { return intervalLength; }
            set {
                intervalLength = value;
                FirePropertyChanged("IntervalLength");
            }
        }

        private int lapse;

        /// <summary>
        /// How many times this item was forgotten
        /// </summary>
        [XmlAttribute]
        public virtual int Lapse {
            get { return lapse; }
            set {
                lapse = value;
                FirePropertyChanged("Lapse");
            }
        }

        private int repetition;

        [XmlAttribute]
        public virtual int Repetition {
            get { return repetition; }
            set {
                repetition = value;
                FirePropertyChanged("Repetition");
            }
        }

        private double difficulty;
        [XmlAttribute]
        public virtual double Difficulty {
            get { return difficulty; }
            set {
                difficulty = value;
                FirePropertyChanged("Difficulty");
            }
        }

        #region Equality Members

        public virtual bool Equals(ItemLearningData other) {
            if (ReferenceEquals(null, other)) {
                return false;
            }
            if (ReferenceEquals(this, other)) {
                return true;
            }
            return other.id == id;
        }

        public override bool Equals(object obj) {
            if (ReferenceEquals(null, obj)) {
                return false;
            }
            if (ReferenceEquals(this, obj)) {
                return true;
            }
            if (obj.GetType() != typeof (ItemLearningData)) {
                return false;
            }
            return Equals((ItemLearningData) obj);
        }

        public override int GetHashCode() {
            return id.GetHashCode();
        }

        #endregion

        public virtual event PropertyChangedEventHandler PropertyChanged;

        private void FirePropertyChanged(string name) {
            if (PropertyChanged != null) {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}